package com.hb.trade.server.center.au.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.dto.UserDTO;
import com.hb.core.pojo.center.au.entity.Org;
import com.hb.core.pojo.center.au.entity.Role;
import com.hb.core.pojo.center.au.entity.User;
import com.hb.core.pojo.center.au.mapper.OrgMapper;
import com.hb.core.pojo.enums.RoleTypeEnum;
import com.hb.trade.server.center.au.service.OrgService;
import com.hb.trade.server.center.au.service.RoleService;
import com.hb.trade.server.center.au.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 组织架构信息表 服务实现类
 *
 * @author zhaojk
 * @since 2021-11-29
 */
@Slf4j
@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements OrgService {

    private final static String EXISTS_ERROR_FORMATTER = "{}: [{}], 该数据已经存在";

    protected final UserRoleService userRoleService;
    protected final RoleService roleService;

    public OrgServiceImpl(UserRoleService userRoleService,
                          RoleService roleService) {
        this.userRoleService = userRoleService;
        this.roleService = roleService;
    }

    /**
     * 判断企业是否存在
     * 只取参数中的orgName和creditCode进行判断
     *
     * @param org
     * @return 数据库中的企业对象
     */
    @Override
    public Org exists(Org org) {
        // 判断信用代码
        String creditCode = org.getCreditCode();
        QueryWrapper<Org> wrapper = Org.wrapper();
        wrapper.eq("credit_code", creditCode).last("limit 1");
        Org rawOrg = getOne(wrapper);
        if (rawOrg != null) {
            log.info(EXISTS_ERROR_FORMATTER, "统一社会信用代码", creditCode);
            return rawOrg;
        }

        // 判断企业名称
        String orgName = org.getOrgName();
        wrapper.clear();
        wrapper.eq("org_name", orgName).last("limit 1");
        rawOrg = getOne(wrapper);
        if (rawOrg != null) {
            log.info(EXISTS_ERROR_FORMATTER, "企业名称", orgName);
            return rawOrg;
        }
        return null;
    }

    /**
     * @param orgId:
     * @Description: 查询当前企业所有的父企业(不包含当前企业)
     * @Author: zhongdl
     * @Date:2022/1/20 9:03
     * @return: {@link List< Org> }
     * @deprecated 方法局限性太大
     **/
    @Deprecated
    @Override
    public List<Org> listParent(Long orgId) {
        List<Org> list = baseMapper.listParent(orgId);
        return list;
    }

    /**
     * @param orgId:
     * @Description: 查询当前企业所有的子企业(包含当前企业)
     * @Author: zhongdl
     * @Date:2022/1/20 9:03
     * @return: {@link List< Org> }
     * @deprecated 方法局限性太大
     **/
    @Deprecated
    @Override
    public List<Org> listSon(Long orgId) {
        List<Org> list = baseMapper.listSon(orgId);
        return list;
    }

    @Override
    public Page<Org> pageTenderOrg(OrgDTO orgDTO) {
        Page<Org> orgPage = baseMapper.pageTenderOrg(orgDTO.pages(), orgDTO);
        return orgPage;
    }

    /**
     * 获取指定企业下的, 指定角色类型的用户
     *
     * @param orgId
     * @param roleType
     * @return
     */
    @Override
    public List<UserDTO> getUser(Long orgId, RoleTypeEnum roleType) {
        // 查询指定企业下的指定角色类型的列表数据
        QueryWrapper<Role> wrapper = Role.wrapper("id", "role_type");
        wrapper.lambda().eq(Role::getOrgId, orgId);
        wrapper.lambda().eq(Role::getRoleType, roleType.getCode());
        List<Role> roles = roleService.list(wrapper);

        // 若不存在任何角色, 返回空
        if (CollUtil.isEmpty(roles)) {
            return CollUtil.list(false);
        }

        Role role = roles.get(0);

        return userRoleService.listMembersByRoleId(role.getId());
    }

    /**
     * 查询总公司
     * @param orgId
     * @return
     */
    @Override
    public OrgDTO getHeadOffice(Long orgId){
        List<Org> orgList = baseMapper.listParent(orgId);
        for (Org org: orgList){
            if (org.getParentId()==null || org.getParentId().equals(0L)){
                return org.to(OrgDTO.class);
            }
        }
        return null;
    }

    /**
     * 查看企业是否绑定了中招
     * @param orgDTO
     * @return
     */
    @Override
    public boolean orgConfirm(OrgDTO orgDTO){
        Integer num = baseMapper.orgConfirmCount(orgDTO);
        if (num>0){
            return true;
        }
        return false;
    }

}
